package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.core.domain.entity.SysUser;
import com.distribution.common.exception.ServiceException;
import com.distribution.common.utils.StringUtils;
import com.distribution.framework.config.ServerConfig;
import com.distribution.system.domain.bo.Recommended;
import com.distribution.system.domain.dto.UpdateSubAgentCommissionByDeductPercentDTO;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.domain.entity.CmccDaProfitCondition;
import com.distribution.system.domain.req.CmccDaProductAddReq;
import com.distribution.system.domain.req.CmccDaProductListReq;
import com.distribution.system.domain.req.CmccDaProductUpdateReq;
import com.distribution.system.domain.req.ReqResult;
import com.distribution.system.domain.vo.PlaceOrderImgVO;
import com.distribution.system.mapper.CmccDaProductMapper;
import com.distribution.system.mapper.CmccDaProfitConditionMapper;
import com.distribution.system.mapper.CmccDaProxyProductManageMapper;
import com.distribution.system.mapper.SysUserMapper;
import com.distribution.system.service.AddressSystemService;
import com.distribution.system.service.CmccDaProductService;
import com.distribution.system.service.CmccDaProfitConditionService;
import com.distribution.system.service.CmccDaProxyProductManageService;
import com.distribution.system.utils.DateUtil;
import com.distribution.system.utils.EncryptionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

import static com.distribution.common.utils.SecurityUtils.getUserId;


/**
 * @author Liping Huo
 * @date 2023/5/22 12:05
 */
@Service
public class CmccDaProductServiceImpl extends ServiceImpl<CmccDaProductMapper, CmccDaProduct> implements CmccDaProductService {
    @Value("${swagger.pathMapping}")
    private String pathMapping;
    @Resource
    ServerConfig serverConfig;
    //默认的推荐卡 链接头地址
    private static final String CARD_RECOMMEND_LINK = "/promotion";
    @Resource
    CmccDaProductMapper cmccDaProductMapper;
    @Resource
    CmccDaProxyProductManageMapper cmccDaProxyProductManageMapper;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    CmccDaProfitConditionService cmccDaProfitConditionService;
    @Resource
    CmccDaProxyProductManageService cmccDaProxyProductManageService;
    @Resource
    AddressSystemService addressSystemService;
    @Resource
    CmccDaProfitConditionMapper cmccDaProfitConditionMapper;



    /**
     * @Author: GodLu
     * @Date: 2024/7/19 0:46
     * @Description: 检查是否为屏蔽地区
     * @param provinceCode 当前收货地址省份编码
     * @param cmccDaProduct 下单产品信息
     * @return: ReqResult
     */
    public ReqResult checkBanArea(String provinceCode, CmccDaProduct cmccDaProduct){
        String limitAreas = cmccDaProduct.getLimitAreas();
        if (!StringUtils.isEmpty(limitAreas)) {
            String[] split = limitAreas.substring(1, limitAreas.length() - 1).replaceAll("\"", "").split(",");
            List<String> limitAreaList = Arrays.asList(split);
            return limitAreaList.contains(provinceCode) ? ReqResult.error(addressSystemService.getProvinceNameByCode(provinceCode) + "地区禁止下单") : ReqResult.success();
        }else {
            return ReqResult.success();
        }
    }

    @Override
    public List<Recommended> getRecommendedList(CmccDaProductListReq cmccDaProductListReq) {
//        System.out.println("cmccDaProductListReq = " + cmccDaProductListReq);
        List<Recommended> recommendedList = new ArrayList<>(1 << 4);
        if (cmccDaProductListReq.getUserId() != null) {
            cmccDaProductListReq.setUserId(cmccDaProductListReq.getUserId());
        } else {
            cmccDaProductListReq.setUserId(getUserId());
        }
        //如果是超级管理员或总运营，显示管理员权限下的list
        if (isAdminUser(cmccDaProductListReq.getUserId()) || isOperationUser(cmccDaProductListReq.getUserId())) {
            //查所有未隐藏
            recommendedList.addAll(cmccDaProductMapper.selectRecommendedAdminUserList(cmccDaProductListReq));
        } else {
            recommendedList.addAll(cmccDaProductMapper.selectRecommendedOtherUserList(cmccDaProductListReq));
        }
        resultOfterProcessList(recommendedList);
        return recommendedList;
    }

    @Override
    public List<Recommended> getCollectProductList(CmccDaProductListReq cmccDaProductListReq) {
        if (cmccDaProductListReq.getUserId() == null) {
            cmccDaProductListReq.setUserId(getUserId());
        }
        List<Recommended> recommendedList = new ArrayList<>(1 << 4);
        recommendedList.addAll(cmccDaProductMapper.selectRecommendedOtherUserList(cmccDaProductListReq));
        resultOfterProcessList(recommendedList);
        return recommendedList;
    }

    @Override
    public PlaceOrderImgVO getPlaceOrderImgByProductCode(String productCode){
        CmccDaProduct cmccDaProduct = super.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode), false);
        if (cmccDaProduct == null) throw new ServiceException("商品编码异常");
        String prefix = serverConfig.getUrl() + pathMapping;
        PlaceOrderImgVO placeOrderImgVO = new PlaceOrderImgVO();
        placeOrderImgVO.setMainImagePath(prefix + cmccDaProduct.getMainImagePath());
        placeOrderImgVO.setShareImagePath(prefix + cmccDaProduct.getShareImagePath());
        placeOrderImgVO.setOriginalCodeImagePath(prefix + cmccDaProduct.getOriginalCodeImagePath());
        placeOrderImgVO.setPlaceOrderBgPath(prefix + cmccDaProduct.getUnorderBgPath());
        return placeOrderImgVO;
    }

    protected String createProductNumber() {
        //获取当前日期
        String dateString = new SimpleDateFormat("yyyyMMdd").format(new Date()).substring(2);
        //得到编号(日期数*倍率+申请编号基础值)
        //查询当天有多少
        Date start = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 00:00:00");
        Date end = DateUtil.strToDateLong(DateUtil.dateToStr(new Date(), Locale.CHINA) + " 23:59:59");
        int count = count(new LambdaUpdateWrapper<CmccDaProduct>().ge(CmccDaProduct::getCreateTime, start).lt(CmccDaProduct::getCreateTime, end));
        int i = Integer.parseInt(dateString) * 1000 + count + 1;
        return String.valueOf(i);
    }

    @Override
    public CmccDaProduct getByProductCode(String productCode) {
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode), false);
        if (one == null) throw new ServiceException("当前产品【" + productCode + "】不存在或存在多条记录");
        return one;
    }

    @Override
    public String getLinkFailureStatus(String productCode) {
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode), false);
        if (one == null) throw new ServiceException("当前产品不存在或存在多条记录");
        return one.getProductStatus();
    }

    @Override
    public Integer getLimitActiveTime(String productCode) {
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode),false);
        if (one == null){
            throw new ServiceException("当前产品不存在或存在多条记录");
        }
        Integer limitActiveTime = one.getLimitActiveTime();
        //System.out.println(limitActiveTime);
        return limitActiveTime == null || limitActiveTime <= 0 ? 0 : limitActiveTime;
    }

    @Override
    public List<String> getLimitAreas(String productCode) {
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode),false);
        if (one == null){
            throw new ServiceException("当前产品不存在或存在多条记录");
        }
        String[] split = one.getLimitAreas().split(",");
        if (split.length == 0){
            return null;
        }
        //System.out.println(Arrays.toString(split));
        return new ArrayList<>(Arrays.asList(split));
    }

    /**
     * @param cmccDaProductListReq 查询条件
     * @Author: GodLu
     * @Date: 2024/7/12 16:17
     * @Description: 根据查询条件获取商品列表
     * @return: void
     */
    @Override
    public List<CmccDaProduct> getProductListByQueryParam(CmccDaProductListReq cmccDaProductListReq) {
        //查询条件
        LambdaQueryWrapper<CmccDaProduct> cmccDaCardWrapper = new LambdaQueryWrapper<>();
        cmccDaCardWrapper.like(!StringUtils.isEmpty(cmccDaProductListReq.getProductName()), CmccDaProduct::getProductName, cmccDaProductListReq.getProductName())
                .eq(!Objects.isNull(cmccDaProductListReq.getProductCode()), CmccDaProduct::getProductCode, cmccDaProductListReq.getProductCode())
                .eq(!Objects.isNull(cmccDaProductListReq.getProductTypeId()), CmccDaProduct::getProductTypeId, cmccDaProductListReq.getProductTypeId())
                .eq(!Objects.isNull(cmccDaProductListReq.getProductStatus()), CmccDaProduct::getProductStatus, cmccDaProductListReq.getProductStatus())
                .eq(!Objects.isNull(cmccDaProductListReq.getBindChannelInfoId()), CmccDaProduct::getBindChannelInfoId, cmccDaProductListReq.getBindChannelInfoId())
                .eq(!Objects.isNull(cmccDaProductListReq.getMergeProductFlag()), CmccDaProduct::getMergeProductFlag, cmccDaProductListReq.getMergeProductFlag())
                .eq(!StringUtils.isBlank(cmccDaProductListReq.getPromoteChannel()), CmccDaProduct::getPromoteChannel, cmccDaProductListReq.getPromoteChannel());
        //排序
        cmccDaCardWrapper.orderByAsc(CmccDaProduct::getSort);
        List<CmccDaProduct> cmccDaProductList = list(cmccDaCardWrapper);
        // 拼接链接
        for (CmccDaProduct cmccDaProduct : cmccDaProductList) {
            if (!StringUtils.isEmpty(cmccDaProduct.getMainImagePath())){
                cmccDaProduct.setMainImagePath(serverConfig.getUrl() + pathMapping + cmccDaProduct.getMainImagePath());
            }
            if (!StringUtils.isEmpty(cmccDaProduct.getShareImagePath())) {
                cmccDaProduct.setShareImagePath(serverConfig.getUrl() + pathMapping + cmccDaProduct.getShareImagePath());
            }
            if (!StringUtils.isEmpty(cmccDaProduct.getOriginalCodeImagePath())) {
                cmccDaProduct.setOriginalCodeImagePath(serverConfig.getUrl() + pathMapping + cmccDaProduct.getOriginalCodeImagePath());
            }
            if (!StringUtils.isEmpty(cmccDaProduct.getUnorderBgPath())) {
                cmccDaProduct.setUnorderBgPath(serverConfig.getUrl() + pathMapping + cmccDaProduct.getUnorderBgPath());
            }
        }
        return cmccDaProductList;
    }

    @Override
    public void removeProductById(Integer id) {
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId, id), false);
        if (one == null) throw new ServiceException("当前产品不存在或存在多条数据");
        cmccDaProductMapper.deleteById(one.getId());
        cmccDaProxyProductManageMapper.deleteByProductCode(one.getProductCode());
    }

    @Override
    @Transactional
    public ReqResult onlineProductByCode(String productCode) {
        // 保证商品存在
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode), false);
        if (one == null) return ReqResult.error("查询不到当前商品");
        // 保证商品下架状态
        if (!"0".equals(one.getProductStatus())) return ReqResult.error("当前商品已经上线");
        // 修改商品状态
        one.setProductStatus("1");
        updateById(one);
        // 显示商品配置信息
        cmccDaProxyProductManageService.updateAllAgentProxyProductStatus(one.getProductCode(),1);
        return ReqResult.success();
    }

    @Override
    @Transactional
    public ReqResult offlineProductByCode(String productCode) {
        // 保证商品存在
        CmccDaProduct one = getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, productCode), false);
        if (one == null) return ReqResult.error("查询不到当前商品");
        // 保证商品上线状态
        if (!"1".equals(one.getProductStatus())) return ReqResult.error("当前商品已经下架");
        // 修改商品状态
        one.setProductStatus("0");
        updateById(one);
        // 隐藏商品配置信息
        cmccDaProxyProductManageService.updateAllAgentProxyProductStatus(one.getProductCode(),0);
        return ReqResult.success();
    }

    @Override
    @Transactional
    public void updateProductById(CmccDaProductUpdateReq cmccDaProductUpdateReq) {
        // 更新之前的商品信息
        CmccDaProduct beforeProduct = getById(cmccDaProductUpdateReq.getId());
        // 更新
        CmccDaProduct cmccDaProduct = new CmccDaProduct();
        BeanUtils.copyProperties(cmccDaProductUpdateReq,cmccDaProduct);
//        System.out.println("cmccDaProduct = " + cmccDaProduct);
        // 查其他产品
        List<CmccDaProduct> otherProductList = list(new LambdaUpdateWrapper<CmccDaProduct>().ne(CmccDaProduct::getId,cmccDaProduct.getId()));
        //如果固定归属地则根据省市编码设置归属地省市名称
        if ("1".equals(cmccDaProduct.getPlaceRegularFlag())) {
            String regularProvinceName = addressSystemService.getProvinceNameByCode(cmccDaProduct.getRegularProvinceCode());
            String regularCityName = addressSystemService.getCityNameByCode(cmccDaProduct.getRegularCityCode());
            if (StringUtils.isBlank(regularProvinceName) || StringUtils.isBlank(regularCityName)) throw new ServiceException("找不到固定归属地地址信息");
            cmccDaProduct.setRegularProvince(regularProvinceName);
            cmccDaProduct.setRegularCity(regularCityName);
        }
        //排序有重复值
        List<CmccDaProduct> collect = otherProductList.stream().filter(ff -> ff.getSort().equals(cmccDaProduct.getSort())).collect(Collectors.toList());
        if (!collect.isEmpty()) {
            List<CmccDaProduct> collect1 = otherProductList.stream().filter(dd -> dd.getSort() >= cmccDaProduct.getSort()).collect(Collectors.toList());
            if (!collect1.isEmpty()) {
                collect1.forEach(gg->gg.setSort(gg.getSort() +1));
                updateBatchById(collect1);
            }
        }
        // 设置屏蔽地区
        cmccDaProduct.setLimitAreas(getLimitAreaStr(cmccDaProductUpdateReq.getLimitAreas()));
        // 设置图片路径
        cmccDaProduct.setMainImagePath(cmccDaProductUpdateReq.getMainImagePath() == null ? null : cmccDaProductUpdateReq.getMainImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setShareImagePath(cmccDaProductUpdateReq.getShareImagePath() == null ? null : cmccDaProductUpdateReq.getShareImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setOriginalCodeImagePath(cmccDaProductUpdateReq.getOriginalCodeImagePath() == null ? null : cmccDaProductUpdateReq.getOriginalCodeImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setUnorderBgPath(cmccDaProductUpdateReq.getUnorderBgPath() == null ? null : cmccDaProductUpdateReq.getUnorderBgPath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        //设置其他值
        cmccDaProduct.setUpdateTime(LocalDateTime.now());
        QueryWrapper<CmccDaProfitCondition> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("profit_condition_id", cmccDaProduct.getProfitConditionId());
        CmccDaProfitCondition cmccDaProfitCondition = cmccDaProfitConditionMapper.selectOne(objectQueryWrapper);
        cmccDaProduct.setProfitConditionDesc(cmccDaProfitCondition.getProfitConditionDesc());
        // 更新
        updateById(cmccDaProduct);
        // 如果产品状态改变则更新产品配置信息
        if (!StringUtils.equals(cmccDaProductUpdateReq.getProductStatus(),beforeProduct.getProductStatus())){
            if ("1".equals(cmccDaProductUpdateReq.getProductStatus())){
                // 上线产品，显示商品配置信息
                cmccDaProxyProductManageService.updateAllAgentProxyProductStatus(beforeProduct.getProductCode(),1);
            } else if ("0".equals(cmccDaProductUpdateReq.getProductStatus())) {
                // 下架产品，隐藏商品配置信息
                cmccDaProxyProductManageService.updateAllAgentProxyProductStatus(beforeProduct.getProductCode(),0);
            }
        }
        // 修改下级代理佣金
        if (!Objects.isNull(cmccDaProductUpdateReq.getBasicsCommission()) && cmccDaProductUpdateReq.getBasicsCommission().compareTo(beforeProduct.getBasicsCommission()) != 0) {
            UpdateSubAgentCommissionByDeductPercentDTO updateSubAgentCommissionByDeductPercentDTO = new UpdateSubAgentCommissionByDeductPercentDTO();
            updateSubAgentCommissionByDeductPercentDTO.setCurrentSysUser(sysUserMapper.selectUserById(getUserId()));
            updateSubAgentCommissionByDeductPercentDTO.setProductCode(beforeProduct.getProductCode());
            updateSubAgentCommissionByDeductPercentDTO.setMyCommission(cmccDaProduct.getBasicsCommission());
            cmccDaProxyProductManageService.updateSubAgentCommissionByDeductPercent(updateSubAgentCommissionByDeductPercentDTO);
        }
    }

    @Override
    @Transactional
    public void addProduct(CmccDaProductAddReq cmccDaProductAddReq) {
        //System.out.println("cmccDaProductAddReq = " + cmccDaProductAddReq);
        CmccDaProduct cmccDaProduct = new CmccDaProduct();
        cmccDaProduct.setLimitAreas(getLimitAreaStr(cmccDaProductAddReq.getLimitAreas()));
        cmccDaProductAddReq.setLimitAreas(null);
        BeanUtils.copyProperties(cmccDaProductAddReq, cmccDaProduct);
        saveProduct(cmccDaProduct);
    }

    @Override
    @Transactional
    public void addMergeProduct(CmccDaProductAddReq cmccDaProductAddReq) {
        CmccDaProduct cmccDaProduct = new CmccDaProduct();
        // 设置屏蔽地区
        cmccDaProduct.setLimitAreas(getLimitAreaStr(cmccDaProductAddReq.getLimitAreas()));
        cmccDaProductAddReq.setLimitAreas(null);
        BeanUtils.copyProperties(cmccDaProductAddReq, cmccDaProduct);
        //设置为聚合商品
        cmccDaProduct.setMergeProductFlag("1");
        saveProduct(cmccDaProduct);
    }

    private String getLimitAreaStr(List<String> limitAreaList){
        StringBuilder limitAreasStrBuilder = new StringBuilder();
        limitAreasStrBuilder.append("[");
        for (int i = 0; i < limitAreaList.size(); i++) {
            limitAreasStrBuilder.append("\"").append(limitAreaList.get(i)).append("\"");
            if (i < limitAreaList.size() - 1){
                limitAreasStrBuilder.append(",");
            }
        }
        limitAreasStrBuilder.append("]");
        return limitAreasStrBuilder.toString();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveProduct(CmccDaProduct cmccDaProduct) {
        //System.out.println("cmccDaProduct = " + cmccDaProduct);
        // 如果已经存在则报错
        List<CmccDaProduct> productList = list(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getProductCode, cmccDaProduct.getProductCode()));
        if (!productList.isEmpty() && !Objects.isNull(productList.get(0))) throw new ServiceException("商品已经存在");
        // 查询数据库中排序权重一样的卡
        List<CmccDaProduct> list = list();
        List<CmccDaProduct> collect = list.stream().filter(ff -> ff.getSort().equals(cmccDaProduct.getSort())).collect(Collectors.toList());
        // 如果存在
        if (!collect.isEmpty()) {
            //查询比当前添加卡的排序权重大的集合
            List<CmccDaProduct> collect1 = list.stream().filter(dd -> dd.getSort() >= cmccDaProduct.getSort()).collect(Collectors.toList());
            //如果有
            if (!collect1.isEmpty()) {
                //权重逐一+1
                collect1.forEach(gg -> gg.setSort(gg.getSort() + 1));
                updateBatchById(collect1);
            }
        }
        // 设置图片路径
        cmccDaProduct.setMainImagePath(cmccDaProduct.getMainImagePath() == null ? null : cmccDaProduct.getMainImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setShareImagePath(cmccDaProduct.getShareImagePath() == null ? null : cmccDaProduct.getShareImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setOriginalCodeImagePath(cmccDaProduct.getOriginalCodeImagePath() == null ? null : cmccDaProduct.getOriginalCodeImagePath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        cmccDaProduct.setUnorderBgPath(cmccDaProduct.getUnorderBgPath() == null ? null : cmccDaProduct.getUnorderBgPath().replaceAll(serverConfig.getUrl() + pathMapping, ""));
        // 如果固定归属地则设置当前卡的所属地区信息
        if ("1".equals(cmccDaProduct.getPlaceRegularFlag())) {
            String regularProvinceName = addressSystemService.getProvinceNameByCode(cmccDaProduct.getRegularProvinceCode());
            String regularCityName = addressSystemService.getCityNameByCode(cmccDaProduct.getRegularCityCode());
            if (StringUtils.isBlank(regularProvinceName) || StringUtils.isBlank(regularCityName)) throw new ServiceException("找不到固定归属地地址信息");
            cmccDaProduct.setRegularProvince(regularProvinceName);
            cmccDaProduct.setRegularCity(regularCityName);
        }
        // 设置当前卡的产品编号、创建信息、更新时间
        String productNumber = createProductNumber();
        cmccDaProduct.setProductCode(productNumber);
        cmccDaProduct.setCreateTime(LocalDateTime.now());
        cmccDaProduct.setUpdateTime(LocalDateTime.now());
        // 设置当前卡的获利条件描述
        CmccDaProfitCondition cmccDaProfitCondition = cmccDaProfitConditionService.getOne(new QueryWrapper<CmccDaProfitCondition>()
                .eq("profit_condition_id", cmccDaProduct.getProfitConditionId()));
        cmccDaProduct.setProfitConditionDesc(cmccDaProfitCondition.getProfitConditionDesc());
        // 设置推荐链接
        cmccDaProduct.setRecommendLink(serverConfig.getUrl() + CARD_RECOMMEND_LINK);
        // 保存
        save(cmccDaProduct);
        // 添加商品配置信息
        cmccDaProxyProductManageService.addProxyProductManageToAllAgent(cmccDaProduct);
    }


    /**
     * 对结果集最后的处理
     *
     * @param recommendedList 链接集合
     */
    private void resultOfterProcessList(List<Recommended> recommendedList) {
        for (Recommended recommended : recommendedList) {
            resultOfterProcess(recommended);
        }
    }

    private void resultOfterProcess(Recommended recommended) {
        recommended.setRecommendedLink(recommended.getRecommendedLink() + "?userId=" + EncryptionUtil.encodeBase64(String.valueOf(recommended.getUserId())) + "&productCode=" + recommended.getProductCode());
        String url = serverConfig.getUrl() + pathMapping;
        recommended.setMainImageLink(url + recommended.getMainImageLink());
        recommended.setShareImageLink(url + recommended.getShareImageLink());
    }

    /**
     * 判断是否为admin用户
     *
     * @return 是返回 true 否择 返回 false
     */
    private boolean isAdminUser(Long userId) {
        //TODO根据用户ID 获取cardId列表
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        Long roleId = sysUser.getRoleId();
        return roleId == 1;
    }

    /**
     * 判断是否运营账户
     *
     * @return 是返回 true 否择 返回 false
     */
    private boolean isOperationUser(Long userId) {
        //TODO根据用户ID 获取cardId列表
        SysUser sysUser = sysUserMapper.selectUserById(userId);
        Long roleId = sysUser.getRoleId();
        return roleId == 3 || roleId == 103 || roleId == 104;
    }
}
